package com.fe.class04;

import java.util.Arrays;

/**
 * @Description 在一个数组中，任何一个前面的数a，和任何一个后面的数b，如果(a,b)是降序的，就称为逆序对。求数组中逆序对个数
 * @Author sosuke :-)
 * @Date 2021/12/16 06:28
 */
public class Code04_ReversePair {

    public static int reverPairNumber(int[] arr) {
        if (null == arr || arr.length < 2) {
            return 0;
        }
        return process(arr, 0, arr.length - 1);
    }

    /**
     * arr[L..R]既要排好序，也要求逆序对数量返回
     *
     * @param arr
     * @param L
     * @param R
     * @return
     */
    private static int process(int[] arr, int L, int R) {
        if (L == R) return 0;
        int M = L + ((R - L) >> 1);
        return process(arr, L, M) +
                process(arr, M + 1, R) +
                merge(arr, L, M, R);
    }

    /**
     * 求每个数右侧有多少数比它小
     */
    private static int merge(int[] arr, int L, int M, int R) {
        int[] help = new int[R - L + 1];
        int i = help.length - 1;
        int p1 = M;
        int p2 = R;
        int ans = 0;
        while (p1 >= L && p2 >= M + 1) {
            if (arr[p1] > arr[p2]) { // 左组大，拷贝左组，产生逆序对
                ans += p2 - M;
                help[i--] = arr[p1--];
            } else { // 相等则拷贝右组，右组大，拷贝右组，不产生逆序对
                help[i--] = arr[p2--];
            }
        }
        while (p1 >= L) {
            help[i--] = arr[p1--];
        }
        while (p2 >= M + 1) {
            help[i--] = arr[p2--];
        }
        System.arraycopy(help, 0, arr, L, help.length);
        return ans;
    }

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 8;
        int maxValue = 100;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = Arrays.copyOf(arr1, arr1.length);
            int res1 = reverPairNumber(arr1);
            int res2 = comparator(arr2);
            if (res1 != res2) {
                System.out.println("Fuck");
                System.out.println(Arrays.toString(arr1));
                System.out.println("res1 = " + res1);
                System.out.println("res2 = " + res2);
                return;
            }
        }
        System.out.println("Nice");
        int[] arr = generateRandomArray(maxSize, maxValue);
        System.out.println(Arrays.toString(arr));
        int res = reverPairNumber(arr);
        System.out.println("res = " + res);
    }

    // for循环方式求逆序对个数
    public static int comparator(int[] arr) {
        if (arr == null || arr.length < 2) {
            return 0;
        }
        int res = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                res += arr[j] < arr[i] ? 1 : 0;
            }
        }
        return res;
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) ((maxValue + 1) * Math.random());
        }
        return arr;
    }
}
